package peer.assist.message;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;

public class ReversePingMessage2 extends Message{
	public short sType=peer.tools.MessageType.sType_ReversePingMessage2;
	byte[] dpData;
	InetAddress dstIp;
	int dstPort;
	int pingSessionId;
	public ReversePingMessage2(int pingSessionId,InetAddress dstIp,int dstPort){
		this.pingSessionId=pingSessionId;
		dpData=new byte[16];
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		System.arraycopy(dstIp.getAddress(), 0, dpData, 4, 4);
		ByteIntSwitch.toByteArray(dstPort, dpData, 8);
		ByteIntSwitch.toByteArray(pingSessionId, dpData, 12);
		dp=new DatagramPacket(dpData,dpData.length);
	}
	public ReversePingMessage2(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		byte[] b=new byte[4];
		System.arraycopy(dpData, 4, b, 0, 4);
		try {
			dstIp=InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		dstPort=ByteIntSwitch.toInt(dpData, 8);
		pingSessionId=ByteIntSwitch.toInt(dpData, 12);
	}
	
	public InetAddress getDstIp(){
		return dstIp;
	}
	
	public int getDstPort(){
		return dstPort;
	}

	public int getPingSessionId(){
		return pingSessionId;
	}
}
